package com.sxran.im.handler;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sxran.im.bean.MessageReqBody;
import com.sxran.im.controller.ImController;
import com.sxran.im.dao.ImConversationMapper;
import com.sxran.im.info.ImConversation;
import com.sxran.im.service.ChatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class GetConversationListCmdHandler extends AbstractCmdHandler<String>{
    @Resource
    private ImConversationMapper imConversationMapper;
    @Resource
    private ChatService chatService;
    @Override
    public Object doBeanService(String json) {
        String curUserId = ImController.getCurUserId();


        LambdaQueryWrapper<ImConversation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ImConversation::getUserId,curUserId);
        // 排序
        queryWrapper.orderByDesc(ImConversation::getUpdateTime);
        // 执行结果
        List<ImConversation> list = null;
        boolean isPage = false;
        if(isPage){
//            IPage<ImChatBody> page = new Page<>(offset, count);
//            list = imConversationMapper.selectList(page,queryWrapper);
        }else{
            list = imConversationMapper.selectList(queryWrapper);
            list.forEach(x->{
                String id = x.getId();
                MessageReqBody req = new MessageReqBody();
//                req.setUserId(curUserId);
                req.setConversation(id);
                req.setOffset(1);
                req.setCount(5); // 前端20条占啦4个屏幕啦，用5条数据。
                x.setMsgList( chatService.selectList(req) );
            });
        }

        return list;
    }
}
